#include <stdio.h>

#define ROWS 6
#define COLS 7
//int iPointsR[ROWS] = {2, 0, 4, 3, 4, 0};           // 各行黑点数和的情况
//int iPointsC[COLS] = {4, 1, 2, 2, 1, 2, 1};        // 各列黑点数和的情况
int iPointsR[ROWS] = {6,5,3,4,5,3};           // 各行黑点数和的情况
int iPointsC[COLS] = {4,3,3,3,5,6,2};        // 各列黑点数和的情况
int iCount, iFound = 0;
int iSumR[ROWS], iSumC[COLS], Grid[ROWS][COLS];

int Set(int iRowNo,int iParentColNo) {
    if(iRowNo == ROWS) {
        for(int iColNo=0; iColNo < COLS && iSumC[iColNo]==iPointsC[iColNo]; iColNo++)
            if(iColNo == COLS-1) {
                printf("\nNo.%d:\n", ++iCount);
                for(int i=0; i < ROWS; i++)
                    for(int j=0; j < COLS; j++)
                        printf("%d%c", Grid[i][j], (j+1) % COLS ? ' ' : '\n');
                iFound = 1;	// iFound = 1
             }
    } else {
		int iColNo = 0;
		if(iParentColNo != -1) iColNo = iParentColNo + 1;
        for(; iColNo < COLS; iColNo++) {
            if(iPointsR[iRowNo] == 0 && iColNo == 0) {
                 Set(iRowNo + 1,-1);
            }else if(Grid[iRowNo][iColNo]==0) {
                Grid[iRowNo][iColNo] = 1;
                iSumR[iRowNo]++; iSumC[iColNo]++;
                if(iSumR[iRowNo]<iPointsR[iRowNo] && iSumC[iColNo]<=iPointsC[iColNo] && iColNo != COLS - 1)
                    Set(iRowNo,iColNo);
				else if(iSumR[iRowNo]==iPointsR[iRowNo] && iRowNo < ROWS)
                    Set(iRowNo + 1,-1);						
				Grid[iRowNo][iColNo] = 0;
				iSumR[iRowNo]--;
				iSumC[iColNo]--;
            }
        }
    }
    return iFound;      // 用于判断是否有解
}
    int main(int argc, char* argv[]) {
        if(!Set(0,-1))
            printf("Failure!");
    }
